package com.backapp.app;

import java.util.*;

import android.content.*;
import android.content.pm.*;
import android.os.*;

public class AppLister extends AsyncTask<Void, Void, List<AppDetails>>
{
	private Context mContext;
	private AppListingCompletedListener mListener;
	
	public 
	AppLister(
			Context context,
			AppListingCompletedListener listener
			)
	{
		mContext = context;
		mListener = listener;
	}
	
	@Override
	protected List<AppDetails> 
	doInBackground(
		Void... params
		)
	{
		List<ResolveInfo> pkgAppsList;
		List<AppDetails> appDetails;
		final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
    	
    	mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
     	pkgAppsList = mContext.getPackageManager().queryIntentActivities( mainIntent, 0);

     	appDetails = new ArrayList<AppDetails>();
 	
 		for (ResolveInfo rInfo : pkgAppsList)
 		{
 			appDetails.add(new AppDetails(rInfo.activityInfo, mContext));
 		}
 		
 		return appDetails;
	}
	
	@Override
	protected void 
	onPostExecute(
		List<AppDetails> result
		)
	{
		super.onPostExecute(result);
		
		mListener.onListingCompleted(result);
	}
}
